<?php
/**
 * 指标管理 
 *
 */ 

class targetAction extends backendAction{
    

	public function index(){
		$user = getUserSession();
		
		switch ($user['roleId']) {
			case 1 : $defaultType = 'city';			break;
			case 3 : $defaultType = 'microArea';	break;
			case 4 : $defaultType = 'people';		break;
			case 7 : $defaultType = 'people';		break;
			default : $defaultType = 'people';
		}
		
		$type = I('type', $defaultType);
		$month= I('month', date('Y-m'));
		
		if($type == 'city'){
			$data = D(MODULE_NAME)->getCityTargets($month);
		}
		
		if($type == 'microArea'){
			$data = D(MODULE_NAME)->getMicroAreaTargets($month);
		}
		
		if($type == 'people'){
			$data = D(MODULE_NAME)->getPepoleTargets($month);
		}
		
		
		$this->assign('roleId', $user['roleId']);
		$this->assign('month',$month);
		$this->assign('type',$type);
		$this->assign('page',$data['page']);
		$this->assign('list',$data['list']);
			
		$this->display();
	}
	
    public function _before_index(){         
        $this->list_relation = true;
        $type = $this->_request('type','intval',1);
        $this->assign('type',$type);
        $this->readyData();
    }
    
    
    
    
    /**
     * 1、取品牌数据
     */ 
    public function readyData(){
        $brandList = F('brand_list');
        $this->assign('brandList',$brandList);
        $typeList = array(
            1=>array('name'=>'地市','file'=>'region',),
            2=>array('name'=>'区县','file'=>'region',),
            3=>array('name'=>'厅店','file'=>'store',)
        );
        $this->assign('typeList',$typeList);
        
    }
    
    
     protected function _search() {
        $map = array();
        ($time_start = $this->_request('time_start', 'trim')) && $map['date'][] = array('egt', $time_start);
        ($time_end = $this->_request('time_end', 'trim')) && $map['date'][] = array('elt', $time_end);
        ($type = $this->_request('type', 'intval',1)) && $map['type'] = $type;
       /* 
        $brandId = $this->_request('brandId', 'trim');
        if($brandId!=''){
            $map['brandId'] = $brandId;
        }
        $userName = $this->_request('userName', 'trim');
        if ($userName) {
            
            $where['userName']  = array('like',"%$userName%");
            $where['realName']  = array('like',"%$userName%");
            $where['_logic'] = 'or';
            $userMap['_complex'] = $where;  
            $id_arr = M('admin')->where($userMap)->getField('id,userName');

            $ids = array_keys($id_arr);
            $ids = implode(',',$ids);
            if(!empty($_SESSION[C('user_session_name')]['underListIds'])){
                $ids .=','.$_SESSION[C('user_session_name')]['underListIds'];
                 
            }
            
            $ids = trim($ids,',');
        
            if(empty($ids)){
               $ids = "-1"; 
            }
               
            $map['userId'] = array('in',$ids);
        }
        $modelName =  $this->_request('modelName', 'trim');
        if ($modelName) {            
            $id_arr = M('model')->where(array('name'=>array('like',$modelName)))->getField('id,name');
            $ids = array_keys($id_arr);
            if(empty($ids)){
               $ids = "-1"; 
            }
            $map['modelId'] = array('in',$ids);
        }     */   
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'modelName' => $modelName,
            'userName' => $userName,
            'type'  => $type,
        ));
        return $map;
    }   
    
    
    public function show(){
        $userId = $this->_request('userId','intval');
        $list = D('target')->relation(true)->where(array('userId'=>$userId))->order('modelId asc ')->select();
        $sumTarget = array_shift($list);
        $this->assign('sumTarget',$sumTarget);
        $this->assign('list',$list);
        $response = $this->fetch();
        $this->ajaxReturn(1, '', $response);
    }
    
}






?>